﻿using G_IS.Comm;
using Plusii.iiWeb;
using System;
using System.Collections.Generic;
using System.Web;

namespace R.DMS.UI
{
    public class LocalGlobal : iiGlobal
    {
        public const char ListSeparator = ',';
        public const string UploadDirectoryName = "Upload";
        public const string AltDateFormat = "yyyy-MM-dd";
        public const string LinkFormat = "<a target='_blank' href='../{0}' title='{1} @ {2}'>{3}</a>";

        /// <summary>
        /// 允许上传文件类型
        /// </summary>
        public static readonly string[] AllowFileTypes = Utils.GetConfigValueByKey("AllowFileTypes").Split(new char[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
        public static readonly string UploadPhysicalPath = LocalGlobal.PhysicalPath + "\\" + LocalGlobal.UploadDirectoryName;
        public static readonly string UploadVirtualPath = LocalGlobal.VirtualPath + "/" + LocalGlobal.UploadDirectoryName;

        #region Cookie

        /// <summary>
        /// 当前登录人类型（内部账号in、客户账号out）
        /// </summary>
        public static string CurrentUserType
        {
            get
            {
                return ReadCookie("DMS_CurrentUserType");
            }
            set
            {
                WriteCookie("DMS_CurrentUserType", value);
            }
        }

        public static string ReadCookie(string name)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            return cookie != null ? HttpUtility.UrlDecode(cookie.Value) : null;
        }

        public static void ClearCookie(string name)
        {
            WriteCookie(name, "", DateTime.Now.AddDays(-1));
        }

        public static void WriteCookie(string name, string value, DateTime expires)
        {
            string encodedValue = HttpUtility.UrlEncode(value);
            HttpContext httpContext = HttpContext.Current;
            if (httpContext.Request.Cookies[name] == null)
            {
                HttpCookie cookie = new HttpCookie(name, encodedValue);
                cookie.Expires = expires;
                httpContext.Response.Cookies.Add(cookie);
            }
            else
            {
                httpContext.Response.Cookies[name].Value = encodedValue;
            }
        }

        public static void WriteCookie(string name, string value)
        {
            WriteCookie(name, value, LocalGlobal.CookieExpires);
        }

        #endregion

    }
}